package itbour.onetouchshow.utils.video;

import android.graphics.Bitmap;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMetadataRetriever;

import com.xiao.nicevideoplayer.VideoDecoder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.singlecase.InchingDataSingleCase;
import itbour.onetouchshow.utils.L_;

/**
 * Created by Administrator on 2018/1/26.
 */

public class EditPlayVideoFrames {


    public static boolean isStop = false;

    /**
     * java 原生抽帧
     *
     * @param input      要抽帧的视频路径
     * @param begin      开始抽帧时间
     * @param end        结束抽帧时间
     * @param timeMargin 抽帧间隔
     * @param width      抽帧最大宽
     * @param height     抽帧最大高
     * @return result map
     * <p>
     * {
     * "code" : 0, 0 标记成功, 非零标记失败
     * "thumbs":[] bitmap 数组
     * }
     */
    public static ArrayList<Bitmap> javaGetVideoFrames(String input, double begin, double end, double timeMargin, int maxSize, int width, int height) {

        // 构建返回值并异常保护
        // HashMap<String, Object> result = new HashMap<>();
        // result.put("code", "-1");
        if (null == input || input.length() <= 2) return new ArrayList<>();
        if (end <= begin) return new ArrayList<>();
        begin = begin <= 0 ? 0 : begin;
        timeMargin = timeMargin <= 0 ? 0 : timeMargin;
        // 读取视频信息
        MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
        metadataRetriever.setDataSource(input);
        double rotation = Double.parseDouble(metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
        double totalTime = Double.parseDouble(metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
        end = end < totalTime ? end : totalTime;
        // end = end < totalTime ? end : totalTime;
        // 循环取图并返回数据
        ArrayList<Bitmap> thumbs = new ArrayList<>();
        //result.put("thumbs", thumbs);
        //int i = 500 * 1000; i <= videoView.getDuration() * 1000; i += 1000 * 1000
        isStop = false;
        for (double i = begin / 1000; i < end / 1000; i += timeMargin) {
            if (isStop) {
                metadataRetriever.release();
                return thumbs;
            }
            if (i >= maxSize) {
                metadataRetriever.release();
                return thumbs;
            }
            long l = System.currentTimeMillis();
            Bitmap bitmap = metadataRetriever.getFrameAtTime((long) (i * 1000 * 1000), MediaMetadataRetriever.OPTION_CLOSEST);
            L_.e("System.currentTimeMillis()" + (System.currentTimeMillis() - l) / 1000.0 + "");

            if (null != bitmap) thumbs.add(bitmap);
        }
        return thumbs;
    }

    public static boolean startclipDecodeVideo(String videoPath, String newVideoPath, long start_time, long clipDuration) {
        //起始时间  截取时长
        boolean finish = new VideoDecoder().decodeVideo(videoPath, newVideoPath, start_time, clipDuration);
        if (finish) {
            // T_.showCustomToast("视频地址：" + newVideoPath);
            // L_.e(newVideoPath + "startDecodeVideo 视频剪切成功");
            return true;
        } else {
            L_.e(newVideoPath + "视频剪切失败");
            return false;
        }
    }


    public static boolean getVideoEncodeWithPath(String input) {
        String format = null;
        MediaExtractor extractor = new MediaExtractor();
        try {
            extractor.setDataSource(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < extractor.getTrackCount(); i++) {
            MediaFormat mediaFormat = extractor.getTrackFormat(i);
            String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
            if (mime.startsWith("video/")) {
                format = mime;
            }
        }
        HomeDetailslBean.AppControl appControl = InchingDataSingleCase.INSTANCE.getAppControl();
        List<String> supportVideoEncode = null;
        if (appControl != null) {
            supportVideoEncode = appControl.getSupportVideoEncode();
        }
        if (supportVideoEncode != null && supportVideoEncode.size() > 0) {
            if (supportVideoEncode.contains(format)) {
                return true;
            }
        }
        return false;
        //return format;
    }

}
